package cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.accessrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.accessrecord.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.accessrecord.AccessRecordDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.convert.accessrecord.AccessRecordConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.accessrecord.AccessRecordService;

@Tag(name = "管理后台 - 门禁记录")
@RestController
@RequestMapping("/fengdong.smartelderly/access-record")
@Validated
public class AccessRecordController {

    @Resource
    private AccessRecordService accessRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建门禁记录")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:access-record:create')")
    public CommonResult<Long> createAccessRecord(@Valid @RequestBody AccessRecordCreateReqVO createReqVO) {
        return success(accessRecordService.createAccessRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新门禁记录")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:access-record:update')")
    public CommonResult<Boolean> updateAccessRecord(@Valid @RequestBody AccessRecordUpdateReqVO updateReqVO) {
        accessRecordService.updateAccessRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除门禁记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:access-record:delete')")
    public CommonResult<Boolean> deleteAccessRecord(@RequestParam("id") Long id) {
        accessRecordService.deleteAccessRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得门禁记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:access-record:query')")
    public CommonResult<AccessRecordRespVO> getAccessRecord(@RequestParam("id") Long id) {
        AccessRecordDO accessRecord = accessRecordService.getAccessRecord(id);
        return success(AccessRecordConvert.INSTANCE.convert(accessRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得门禁记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:access-record:query')")
    public CommonResult<List<AccessRecordRespVO>> getAccessRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<AccessRecordDO> list = accessRecordService.getAccessRecordList(ids);
        return success(AccessRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得门禁记录分页")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:access-record:query')")
    public CommonResult<PageResult<AccessRecordRespVO>> getAccessRecordPage(@Valid AccessRecordPageReqVO pageVO) {
        PageResult<AccessRecordDO> pageResult = accessRecordService.getAccessRecordPage(pageVO);
        return success(AccessRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出门禁记录 Excel")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:access-record:export')")
    @OperateLog(type = EXPORT)
    public void exportAccessRecordExcel(@Valid AccessRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AccessRecordDO> list = accessRecordService.getAccessRecordList(exportReqVO);
        // 导出 Excel
        List<AccessRecordExcelVO> datas = AccessRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "门禁记录.xls", "数据", AccessRecordExcelVO.class, datas);
    }

}
